home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / xmlproc_parse.python-xml < prev    next >
Encoding:
Text File  |  2007-03-31  |  3.1 KB  |  122 lines

  1. #! /usr/bin/python
  2.  
  3. """
  4. A command-line interface to the xmlproc parser. It continues parsing
  5. even after fatal errors, in order to be find more errors, since this
  6. does not mean feeding data to the application after a fatal error
  7. (which would be in violation of the spec).
  8. """
  9.  
  10. usage=\
  11. """        
  12. Usage:
  13.  
  14.   xpcmd.py [options] [urltodoc]
  15.  
  16.   ---Options:  
  17.   -l language: ISO 3166 language code for language to use in error messages
  18.   -o format:   Format to output parsed XML. 'e': ESIS, 'x': canonical XML
  19.                and 'n': normalized XML. No data will be output if this
  20.                option is not specified.
  21.   urltodoc:    URL to the document to parse. (You can use plain file names
  22.                as well.) Can be omitted if a catalog is specified and contains
  23.                a DOCUMENT entry.
  24.   -n:          Report qualified names as 'URI name'. (Namespace processing.)
  25.   --nowarn:    Don't write warnings to console.
  26.   --entstck:   Show entity stack on errors.
  27.   --extsub:    Read the external subset of documents.
  28. """
  29.  
  30. # --- INITIALIZATION
  31.  
  32. import sys,getopt
  33. from xml.parsers.xmlproc import xmlproc, _outputters
  34.  
  35. # --- Interpreting options
  36.  
  37. try:
  38.     (options,sysids)=getopt.getopt(sys.argv[1:],"l:o:n",
  39.                                    ["nowarn","entstck","rawxml","extsub"])
  40. except getopt.error,e:
  41.     print "Usage error: "+e.msg
  42.     print usage
  43.     sys.exit(1)
  44.     
  45. pf=None
  46. namespaces=0
  47. app=xmlproc.Application()
  48. warnings=1
  49. entstack=0
  50. rawxml=0
  51. extsub=0
  52.  
  53. p=xmlproc.XMLProcessor()
  54.  
  55. for option in options:
  56.     if option[0]=="-l":
  57.         try:
  58.             p.set_error_language(option[1])
  59.         except KeyError:
  60.             print "Error language '%s' not available" % option[1]
  61.     elif option[0]=="-o":
  62.         if option[1]=="e" or option[1]=="E":
  63.             app=_outputters.ESISDocHandler()            
  64.         elif option[1]=="x" or option[1]=="X":
  65.             app=_outputters.Canonizer()
  66.         elif option[1]=="n" or option[1]=="N":
  67.             app=_outputters.DocGenerator()
  68.         else:
  69.             print "Error: Unknown output format "+option[1]
  70.             print usage
  71.     elif option[0]=="-n":
  72.         namespaces=1
  73.     elif option[0]=="--nowarn":
  74.         warnings=0
  75.     elif option[0]=="--entstck":
  76.         entstack=1
  77.     elif option[0]=="--rawxml":
  78.         rawxml=1
  79.     elif option[0]=="--extsub":
  80.         extsub=1
  81.  
  82. # Acting on option settings
  83.  
  84. err=_outputters.MyErrorHandler(p, p, warnings, entstack, rawxml)
  85. p.set_error_handler(err)
  86.  
  87. if namespaces:
  88.     from xml.parsers.xmlproc import namespace
  89.  
  90.     nsf=namespace.NamespaceFilter(p)
  91.     nsf.set_application(app)
  92.     p.set_application(nsf)
  93. else:
  94.     p.set_application(app)
  95.  
  96. if len(sysids)==0:
  97.     print "You must specify a file to parse"
  98.     print usage
  99.     sys.exit(1)
  100.  
  101. if extsub:
  102.     p.set_read_external_subset(extsub)
  103.     
  104. # --- Starting parse    
  105.  
  106. print "xmlproc version %s" % xmlproc.version
  107.  
  108. for sysid in sysids:
  109.     print
  110.     print "Parsing '%s'" % sysid
  111.     p.set_data_after_wf_error(0)
  112.     p.parse_resource(sysid)
  113.     print "Parse complete, %d error(s)" % err.errors,
  114.  
  115.     if warnings:
  116.         print "and %d warning(s)" % err.warnings
  117.     else:
  118.         print
  119.     
  120.     err.reset()
  121.     p.reset()
  122.